<template>
  <div v-if="!isWaitingStatus" class="settle-middle h-full">
    <div class="settle-middle-box h-full flex flex-col justify-center items-center">
      <!-- 设计师 23，@/utils/enums/system 记录-->
      <designer-form
        v-show="handleShowDesignerForm()"
        ref="designerFormRef"
        :defaultForm="defaultFormDesigner"
        :subject-type-list="subjectTypeList"
        @submitSuccess="handleSubmitSuccess"
      ></designer-form>
      <merchant-form
        v-show="!handleShowDesignerForm()"
        ref="merchantFormRef"
        :defaultForm="defaultFormMerchant"
        :subject-type-list="subjectTypeList"
        @submitSuccess="handleSubmitSuccess"
      ></merchant-form>
    </div>
  </div>
  <wait v-else></wait>
</template>
<script setup>
import { ElNotification } from "element-plus";
// 存储
import { storeGetUserType, storeGetMerchantStatus, storeSetMerchantStatus } from "@/utils/auth";
// 组件
import Wait from "./wait.vue";
import MerchantForm from "./modules/MerchantForm.vue";
import DesignerForm from "./modules/DesignerForm.vue";
// 枚举
import { SUBJECT_TYPE_ENUM, SYSTEM_TYPE_REVERSE_OBJECT } from "@/utils/enums/system";
import { useUserStore } from "@/store/modules/user";
import { useRouter } from "vue-router";
const router = useRouter();
const userStore = useUserStore();
// const userInfo = useUserStore().userInfo
// 用户角色类型
const userType = storeGetUserType();
// 显示进度，入驻成功还是失败，还是没审核  在enums已经定义
// 如果是未审核，则显示入驻进度，如果显示是失败，需要获取之前的信息
// 景瑞说直接放在了userInfo.status中，如果变成对象就直接取
/**
 * 获取当前页面的状态
 *
 */
// status
// "-1": "未找到": 说明要填写资料
//   0: "未审核": 资料还在等待审批
//   1: "入驻通过": 资料审核通过
//   2: "入驻失败": 资料审核失败, 需要重新填写
const isWaitingStatus = ref(false);
const defaultFormDesigner = ref({});
const defaultFormMerchant = ref({});
const funcGetMerchantStatus = async () => {
  try {
    let merchantStatus = storeGetMerchantStatus();
    // 0 代表正在等待审核，跳转等待页面
    isWaitingStatus.value = merchantStatus == 0 ? true : false;
    let remark = "";
    const isDesigner = [SYSTEM_TYPE_REVERSE_OBJECT.设计师, SYSTEM_TYPE_REVERSE_OBJECT.项目经理];
    if (isDesigner.includes(userType)) {
      remark = defaultFormDesigner.value.remark;
    } else {
      remark = defaultFormMerchant.value.remark;
    }
    // 2 代表审核失败，需要补充表单资料
    if (merchantStatus == 2) {
      ElNotification({
        type: "error",
        title: "审核失败",
        message: `失败原因: ${remark}`,
        duration: 0,
      });
    }
  } catch (err) {
    console.log(err, "getMerchantStatus");
  }
};
const handleShowDesignerForm = () => {
  const isDesigner = [SYSTEM_TYPE_REVERSE_OBJECT.设计师, SYSTEM_TYPE_REVERSE_OBJECT.项目经理];
  if (isDesigner.includes(userType)) {
    defaultFormDesigner.value = userStore.getUserSettleInfo();
  } else {
    defaultFormMerchant.value = userStore.getUserSettleInfo();
  }
  return isDesigner.includes(userType);
};
/**
 * 根据用户角色去获取主体类型列表 - 前端自己存储的枚举
 *
 */
const subjectTypeList = ref([]);
subjectTypeList.value = SUBJECT_TYPE_ENUM[userType];
//subjectTypeList.value = SUBJECT_TYPE_ENUM[21]
/***
 *  商户的表单
 *
 */
const handleSubmitSuccess = () => {
  isWaitingStatus.value = true;
  // 设置状态为0，表示正在等待审核
  storeSetMerchantStatus(0);
};
watch(isWaitingStatus, (newVal) => {
  if (newVal) {
    setTimeout(async () => {
      await userStore.logout();
      router.push("/login");
    }, 5000);
  }
});
onMounted(() => {
  funcGetMerchantStatus();
});
</script>
